package com.cph.project.controller;

import java.util.List;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cph.project.pojo.OaProjectOpportunityPortrait;
import com.cph.project.service.IOaProjectOpportunityPortraitService;
import com.cph.common.http.HttpResponseResult;
import com.cph.common.http.HttpStatus;
import com.cph.framework.page.TableDataInfo;

/**
 * 项目机会画像Controller
 *
 * @author cph
 * @date 2020-02-26
 */
@RestController
@RequestMapping("/project/portrait")
public class OaProjectOpportunityPortraitController {
    @Autowired
    private IOaProjectOpportunityPortraitService oaProjectOpportunityPortraitService;

    /**
     * 查询项目机会画像列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OaProjectOpportunityPortrait oaProjectOpportunityPortrait) {
        List<OaProjectOpportunityPortrait> list = oaProjectOpportunityPortraitService.selectOaProjectOpportunityPortraitList(oaProjectOpportunityPortrait);
        return getDataTable(list);
    }

    /**
     * 获取项目机会画像详细信息
     */
    @GetMapping(value = "/{id}")
    public HttpResponseResult getInfo(@PathVariable("id") String id) {
        return HttpResponseResult.success(oaProjectOpportunityPortraitService.selectOaProjectOpportunityPortraitById(id));
    }

    /**
     * 新增项目机会画像
     */
    @PostMapping
    public HttpResponseResult add(@RequestBody OaProjectOpportunityPortrait oaProjectOpportunityPortrait) {
        return toHttpResponseResult(oaProjectOpportunityPortraitService.insertOaProjectOpportunityPortrait(oaProjectOpportunityPortrait));
    }

    /**
     * 修改项目机会画像
     */
    @PutMapping
    public HttpResponseResult edit(@RequestBody OaProjectOpportunityPortrait oaProjectOpportunityPortrait) {
        return toHttpResponseResult(oaProjectOpportunityPortraitService.updateOaProjectOpportunityPortrait(oaProjectOpportunityPortrait));
    }

    /**
     * 删除项目机会画像
     */
    @DeleteMapping("/{ids}")
    public HttpResponseResult remove(@PathVariable String[] ids) {
        return toHttpResponseResult(oaProjectOpportunityPortraitService.deleteOaProjectOpportunityPortraitByIds(ids));
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
 * 响应返回结果
 *
 * @param rows 影响行数
 * @return 操作结果
 */
    protected HttpResponseResult toHttpResponseResult(int rows) {
        return rows > 0 ? HttpResponseResult.success() : HttpResponseResult.error();
    }
}
